﻿public class Solution {
    public bool IsHappy(int n)
    {
        HashSet<int> hashNums = new HashSet<int>();
        int temp = 0;
        while (n != 1)
        {
            if (hashNums.Contains(n))
            {
                return false;
            }

            hashNums.Add(n);
            while (n != 0)
            {
                temp += (int)Math.Pow(n % 10, 2);
                n /= 10;
            }

            n = temp;
            temp = 0;
        }

        return true;
    }

    public static void Main(string[] args)
    {
        int n = 19;

        Solution solution = new Solution();
        bool result = solution.IsHappy(n);
        Console.WriteLine(result);
    }
}